package com.firstcare.android_dylibrary.bean;

import com.alibaba.fastjson.JSONObject;
import com.firstcare.android_dylibrary.DYApplication;
import com.firstcare.android_dylibrary.Encrypt.DYEncryptionTools;
import com.firstcare.android_dylibrary.bean.base.BaseModelProxy;
import com.firstcare.android_dylibrary.network.URLSet;
import com.firstcare.android_dylibrary.network.request.DYRequest;
import com.firstcare.android_dylibrary.network.request.ModelRequest;
import com.rdxer.xxlibrary.HTTPUtils.listener.ErrorListener;
import com.rdxer.xxlibrary.HTTPUtils.listener.FailedListener;
import com.rdxer.xxlibrary.HTTPUtils.listener.OKListener;
import com.rdxer.xxlibrary.utils.Log;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by LXF on 16/6/8.
 */

public class AccountProxy extends BaseModelProxy<Account> {


    public AccountProxy(String account, String password) {
        super();
        this.setModel(new Account(account, password));
    }
    public  AccountProxy(){
    }

    public static ArrayList<Account> queryDB(){
        return getDB().query(Account.class);
    }



    public void login(final OKListener<AccountProxy> okListener, FailedListener failedListener, ErrorListener errorListener) {
        JSONObject parames = new JSONObject();
        parames.put("account", getModel().getAccount());
        parames.put("password", getModel().getPassword());
        ModelRequest<Account> request = new ModelRequest<Account>(URLSet.login, parames, new OKListener<Account>() {
            @Override
            public void onOK(Account res, JSONObject response) {
                getDB().save(res);
                /// 登陆成功需要处理的事情
                AccountProxy.this.setModel(res);
                AccountProxy.this.makeCurrentAccount();
                okListener.onOK(AccountProxy.this, response);
            }
        }, failedListener, errorListener) {
            @Override
            public JSONObject preprocessRequestBody(JSONObject requestBody) throws Exception {
                String password = requestBody.getString("password");
                requestBody.put("password", DYEncryptionTools.encryption2_1(password, this.getTimestamp()));
                return requestBody;
            }
            @Override
            protected Object getTargetData(JSONObject response) throws Exception {
                return response.getJSONObject(getKey_data()).getJSONObject("account");
            }
        };
        DYApplication.getShared().getApi().addToRequestQueue(request);
    }

    public void getAccountProxy(final OKListener<AccountProxy> okListener, FailedListener failedListener, ErrorListener errorListener) {
        JSONObject parames = new JSONObject();
        parames.put("access_token", getModel().getAccess_token());
        ModelRequest<AccountProxy> request = new ModelRequest<AccountProxy>(URLSet.getAccountInfo, parames,okListener, failedListener, errorListener) {
            @Override
            protected Object getTargetData(JSONObject response) throws Exception {
                return response.get("account");
            }
        };
        DYApplication.getShared().getApi().addToRequestQueue(request);
    }

    /**
     * 注册获取验证码
     * @param phone
     * @param okListener
     * @param failedListener
     * @param errorListener
     */

       public  void getCode(String phone, final OKListener<Object> okListener, FailedListener failedListener, ErrorListener errorListener){
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("phone",phone);

        DYRequest<Object> request = new DYRequest<Object>(URLSet.getRegistCode, jsonObject, new OKListener<Object>() {
            @Override
            public void onOK(Object res, JSONObject response) {
                /// 登陆成功需要处理的事情
                Log.e(response);
                okListener.onOK(res,response);
            }
        }, failedListener, errorListener) {
            @Override
            protected Object getTargetData(JSONObject response) throws Exception {
                return response;
            }
        };
        DYApplication.getShared().getApi().addToRequestQueue(request);
    }
    /**
     * 添加报表
     */
    public  void postWater(String name,String remark,String hardness,String alkalinity,String cl,String po,String ph, final OKListener<Object> okListener, FailedListener failedListener, ErrorListener errorListener){
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("projectId",100);
        jsonObject.put("laboratoryName",name);
        jsonObject.put("remark",remark);
        jsonObject.put("hardness",hardness);
        jsonObject.put("alkalinity",alkalinity);
        jsonObject.put("cl",cl);
        jsonObject.put("po43",po);
        jsonObject.put("ph",ph);
        DYRequest<Object> request = new DYRequest<Object>(URLSet.waterAddWork, jsonObject, new OKListener<Object>() {
            @Override
            public void onOK(Object res, JSONObject response) {
                /// 登陆成功需要处理的事情
                Log.e(response);
                okListener.onOK(res,response);
            }
        }, failedListener, errorListener) {
            @Override
            protected Object getTargetData(JSONObject response) throws Exception {
                return response;
            }
        };
        DYApplication.getShared().getApi().addToRequestQueue(request);
    }
    /**
     * 注册用户
     *
     */
    public  void registerAccount(String phone, String authCode,String password,final OKListener<Object> okListener, FailedListener failedListener, ErrorListener errorListener){
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("tel",phone);
        jsonObject.put("authCode",authCode);
        jsonObject.put("password",password);
        DYRequest<Object> request = new DYRequest<Object>(URLSet.registAccount, jsonObject, new OKListener<Object>() {
            @Override
            public void onOK(Object res, JSONObject response) {
                /// 登陆成功需要处理的事情
                Log.e(response);
                okListener.onOK(res,response);
            }
        }, failedListener, errorListener) {
            @Override
            public JSONObject preprocessRequestBody(JSONObject requestBody) throws Exception {
                String password = requestBody.getString("password");
                requestBody.put("password", DYEncryptionTools.encryption1(password));
                return requestBody;
            }
            @Override
            protected Object getTargetData(JSONObject response) throws Exception {
                return response;
            }
        };
        DYApplication.getShared().getApi().addToRequestQueue(request);
    }

    /**
     * 找回密码获取验证码
     * @param phone
     * @param okListener
     * @param failedListener
     * @param errorListener
     */

    public  void getFindPasswordCode(String phone, final OKListener<Object> okListener, FailedListener failedListener, ErrorListener errorListener){
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("phone",phone);

        DYRequest<Object> request = new DYRequest<Object>(URLSet.getFotRegistCode, jsonObject, new OKListener<Object>() {
            @Override
            public void onOK(Object res, JSONObject response) {
                /// 登陆成功需要处理的事情
                Log.e(response);
                okListener.onOK(res,response);
            }
        }, failedListener, errorListener) {
            @Override
            protected Object getTargetData(JSONObject response) throws Exception {
                return response;
            }
        };
        DYApplication.getShared().getApi().addToRequestQueue(request);
    }
    /**
     * 注册用户
     *
     */
    public  void findPassword( String authCode,String password,final OKListener<Object> okListener, FailedListener failedListener, ErrorListener errorListener){
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("authCode",authCode);
        jsonObject.put("password",password);
        DYRequest<Object> request = new DYRequest<Object>(URLSet.fotPasswordAccount, jsonObject, new OKListener<Object>() {
            @Override
            public void onOK(Object res, JSONObject response) {
                /// 登陆成功需要处理的事情
                Log.e(response);
                okListener.onOK(res,response);
            }
        }, failedListener, errorListener) {
            @Override
            public JSONObject preprocessRequestBody(JSONObject requestBody) throws Exception {
                String password = requestBody.getString("password");
                requestBody.put("password", DYEncryptionTools.encryption1(password));
                return requestBody;
            }
            @Override
            protected Object getTargetData(JSONObject response) throws Exception {
                return response;
            }
        };
        DYApplication.getShared().getApi().addToRequestQueue(request);
    }

    /**
     * 更改密码
     *
     */
    public  void putPassword( String text,String text1,final OKListener<Object> okListener, FailedListener failedListener, ErrorListener errorListener){
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("id",DYApplication.getShared().getAccountProxy().getModel().getId());
        jsonObject.put("password",text);
        jsonObject.put("newPassword",text1);
        DYRequest<Object> request = new DYRequest<Object>(URLSet.putPassword, jsonObject, new OKListener<Object>() {
            @Override
            public void onOK(Object res, JSONObject response) {
                /// 登陆成功需要处理的事情
                Log.e(response);
                okListener.onOK(res,response);
            }
        }, failedListener, errorListener) {
            @Override
            public JSONObject preprocessRequestBody(JSONObject requestBody) throws Exception {
                String password = requestBody.getString("password");
                String newPassword = requestBody.getString("newPassword");
                requestBody.put("password", DYEncryptionTools.encryption1(password));
                requestBody.put("newPassword", DYEncryptionTools.encryption1(newPassword));
                return requestBody;
            }
            @Override
            protected Object getTargetData(JSONObject response) throws Exception {
                return response;
            }
        };
        DYApplication.getShared().getApi().addToRequestQueue(request);
    }
    /**
     * 获取点检首页的数据
     */
    public  void postWorkorder( String text,final OKListener<Object> okListener, FailedListener failedListener, ErrorListener errorListener){
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("createTime",text);
        jsonObject.put("status",01);
        DYRequest<Object> request = new DYRequest<Object>(URLSet.queryWorkList, jsonObject, new OKListener<Object>() {
            @Override
            public void onOK(Object res, JSONObject response) {
                /// 登陆成功需要处理的事情
                Log.e(response);
                okListener.onOK(res,response);
            }
        }, failedListener, errorListener) {
            @Override
            protected Object getTargetData(JSONObject response) throws Exception {
                return response;
            }
        };
        DYApplication.getShared().getApi().addToRequestQueue(request);
    }
    /**
     * 更改名称
     *
     */
    public  void putName( String text,final OKListener<Object> okListener, FailedListener failedListener, ErrorListener errorListener){
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("id",DYApplication.getShared().getAccountProxy().getModel().getId());
        jsonObject.put("nickname",text);
        DYRequest<Object> request = new DYRequest<Object>(URLSet.putName, jsonObject, new OKListener<Object>() {
            @Override
            public void onOK(Object res, JSONObject response) {
                /// 登陆成功需要处理的事情
                Log.e(response);
                okListener.onOK(res,response);
            }
        }, failedListener, errorListener) {
            @Override
            protected Object getTargetData(JSONObject response) throws Exception {
                return response;
            }
        };
        DYApplication.getShared().getApi().addToRequestQueue(request);
    }
    /**
     * 更改名称
     *
     */
    public  void postSuggsion( String text,final OKListener<Object> okListener, FailedListener failedListener, ErrorListener errorListener){
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("accountId",DYApplication.getShared().getAccountProxy().getModel().getId());
        jsonObject.put("content",text);
        jsonObject.put("type","1");
        jsonObject.put("score",null);
        DYRequest<Object> request = new DYRequest<Object>(URLSet.postSuggest, jsonObject, new OKListener<Object>() {
            @Override
            public void onOK(Object res, JSONObject response) {
                /// 登陆成功需要处理的事情
                Log.e(response);
                okListener.onOK(res,response);
            }
        }, failedListener, errorListener) {
            @Override
            protected Object getTargetData(JSONObject response) throws Exception {
                return response;
            }
        };
        DYApplication.getShared().getApi().addToRequestQueue(request);
    }
    /////////////////////////////////////////////////////////////////////////////////
    public static void checkAccount(){
        List<Account> list = DYApplication.getShared().getDb().query(Account.class);
        if (list!=null ){
            if (list.size()==1){
                Account account =list.get(0);
                AccountProxy accountProxy = new AccountProxy();
                accountProxy.setModel(account);
                DYApplication.getShared().setAccountProxy(accountProxy);
            }
        }
    }

    public void makeCurrentAccount() {
        DYApplication.getShared().setAccountProxy(this);
    }

    public void logout() {
        DYApplication.getShared().setAccountProxy(null);
    }

    public static boolean loginOut(){
          int flag = DYApplication.getShared().getDb().delete(Account.class);
           return flag!=-1;
    }
    public static boolean isLogin() {
        List<Account> list = DYApplication.getShared().getDb().query(Account.class);
        if (list!=null ){
            if (list.size()==1){
                Account account =list.get(0);
                AccountProxy accountProxy = new AccountProxy();
                accountProxy.setModel(account);
                DYApplication.getShared().setAccountProxy(accountProxy);
            }
        }
        return !(DYApplication.getShared().getAccountProxy() == null || DYApplication.getShared().getAccountProxy().getModel() == null);
    }

    public static String getCurrentUserID() {
        if (isLogin() == false) {
            return "0";
        }
        return DYApplication.getShared().getAccountProxy().getModel().getId() + "";
    }

    public static String getCurrentUserAccess_token() {
        if (isLogin() == false) {
            return null;
        }
        return DYApplication.getShared().getAccountProxy().getModel().getAccess_token();
    }

}
